package org.atovio.quickweb.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.atovio.quickweb.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author AtoVio, Winter Yuan
 */
@Controller
public class JSONViewController {

    @RequestMapping("/exam/json1")
    public void responseJSON(HttpServletResponse response) throws IOException {
        // {"name" : "Violet", "age" : 14}
        String json = "{\"name\" : \"Violet\", \"age\" : 14}";

        // 响应回去
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
    }

    @RequestMapping("/exam/json2")
    @ResponseBody
    public User responseUserJSON(HttpServletResponse response) {
        response.setContentType("application/json;charset=utf-8");
        List<String> list = new ArrayList<>();
        list.add("打字");
        list.add("写诗");
        list.add("旅行");
        list.add("打伞晒太阳");
        return new User("Violet", "女", 14, list);
    }

}
